package org.zhxy.zhxy.controller.backend;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.zhxy.zhxy.dao.UserDao;
import org.zhxy.zhxy.entity.Notice;
import org.zhxy.zhxy.entity.User;
import org.zhxy.zhxy.service.UserService;

import org.zhxy.zhxy.utils.MD5Utils;
import org.zhxy.zhxy.utils.PageResult;
import org.zhxy.zhxy.utils.Result;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 项目名称: zhxy
 * 作者: zhaojs
 * 创建日期: 2024年10月20日
 * 文件说明: 见类描述
 */
@RestController
@RequestMapping("/api/admin")
public class AdminUserController {

    @Resource
    private UserService userService;

    @Autowired
    private UserDao userDao;

    /**
     * 新增用户
     */
    @PostMapping("/user/add")
    public Result addUser(@Valid @RequestBody User user){
        if (user.getPassword()!=null){
            user.setPassword(MD5Utils.encodeByMd5WithCipher(user.getPassword()));
        }
        user.setCreateTime(new Date());
        try {
            return userService.save(user) ? Result.success() : Result.fail("新增用户失败");
        } catch (DataIntegrityViolationException e) {
            return Result.fail("学号或工号已存在！");
        }
    }

    /**
     * 修改用户
     */
    @PostMapping("/user/update")
    public Result updateUser(@Valid @RequestBody User user){
        if (user.getPassword()!=null){
            user.setPassword(MD5Utils.encodeByMd5(user.getPassword()));
        }
        user.setUpdateTime(new Date());
        return userService.updateById(user) ? Result.success() : Result.fail("修改用户失败");
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/user/delete")
    public Result deleteUser(@NotNull(message = "id不能为空") Integer id){
        return userService.removeById(id) ? Result.success() : Result.fail("删除用户失败");
    }

    /**
     * 批量删除用户
     */
    @DeleteMapping("/user/deleteBatch")
    public Result deleteBatchUser(@NotEmpty(message = "id列表不能为空") @RequestBody List<Integer> ids){
        return userService.removeByIds(ids) ? Result.success() : Result.fail("批量删除用户失败");
    }

    /**
     * 分页查询所有用户
     */
    @GetMapping("/user/list")
    public Result listUser(@NotNull(message = "页码不能为空") int page, @NotNull(message = "页码不能为空") int pageSize){
        Page<User> userPage = userService.query()
                .orderByDesc("create_time")
                .page(new Page<>(page, pageSize));

        List<User> userList = userPage.getRecords().stream()
                .peek(user -> user.setPassword(null))
                .collect(Collectors.toList());
        PageResult<User> pageResult = new PageResult<>(userPage.getTotal(), userList);
        return Result.success(pageResult);
    }

    /**
     * 根据姓名查询用户
     */
    @GetMapping("/user/queryByName")
    public Result queryUserByName(String username, @NotNull(message = "页码不能为空") int page, @NotNull(message = "页码不能为空") int pageSize) {
        Page<User> userPage = userService.query()
                .like("username", "%" + username + "%")
                .orderByDesc("create_time")
                .page(new Page<>(page, pageSize));

        List<User> userList = userPage.getRecords().stream()
                .peek(user -> user.setPassword(null))
                .collect(Collectors.toList());
        PageResult<User> pageResult = new PageResult<>(userPage.getTotal(), userList);
        return Result.success(pageResult);
    }

}
